package vn.anhqn.englishsentensetranslator.addsentense;

import vn.anhqn.englishsentensetranslator.R;
import vn.anhqn.englishsentensetranslator.model.Category;
import vn.anhqn.englishsentensetranslator.model.Sentense;
import vn.anhqn.englishsentensetranslator.utils.BaseDialog;
import vn.anhqn.englishsentensetranslator.utils.Utils;
import vn.anhqn.englishsentensetranslator.widget.HeverticalTextView;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddCatDialog extends BaseDialog implements android.view.View.OnClickListener {
	private EditText editCatName;
	private Category editCat;
	private HeverticalTextView tvTitle;

	public AddCatDialog(Context context) {
		super(context);
	}

	private AddCatListener addListener;

	public void setAddListener(AddCatListener addListener) {
		this.addListener = addListener;
	}

	public void setEditCat(Category cat) {
		this.editCat = cat;
	}

	@Override
	protected int getLayoutResId() {
		return R.layout.dialog_add_cat;
	}

	@Override
	protected void onCreateContent(View pContentView) {
		setCanceledOnTouchOutside(true);
		findViewById(pContentView, R.id.imv_close).setOnClickListener(this);
		editCatName = findViewById(pContentView, R.id.edit_cat_name);
		tvTitle = findViewById(pContentView, R.id.tv_title_dialog);
		Button btnAdd = findViewById(pContentView, R.id.btn_add);
		btnAdd.setOnClickListener(this);
		Utils.getIntance().setFontFromAsset(btnAdd, "Helvetica/helvetica-normal.ttf");
		Button btnCancel = findViewById(pContentView, R.id.btn_cancel);
		btnCancel.setOnClickListener(this);
		Utils.getIntance().setFontFromAsset(btnCancel, "Helvetica/helvetica-normal.ttf");
		if (editCat != null) {
			editCatName.setText(editCat.getName());
			btnAdd.setText(R.string.btn_save);
			tvTitle.setText(R.string.dialog_edit_cat);

		}

	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch (id) {
		case R.id.imv_close:
			dismiss();
			break;
		case R.id.btn_cancel:
			dismiss();
			break;

		case R.id.btn_add:
			if (editCatName.getText() != null && !editCatName.getText().toString().isEmpty()) {
				if (editCat == null) {
					if (addListener != null)
						addListener.onAddClick(editCatName.getText().toString());
				} else {
					editCat.setName(editCatName.getText().toString());
					if (addListener != null)
						addListener.onEdit(editCat);
				}
				dismiss();
			} else {
				Toast.makeText(getContext(), R.string.add_cat_empty, Toast.LENGTH_SHORT).show();
			}
			break;
		default:
			break;
		}

	}

	/*
	 * private void setWidthForSpinner(SpinnerFilterEventAdapter adapter,
	 * Spinner spinner) { if (spinner != null && adapter != null) {
	 * adapter.setSpinner(spinner); } }
	 */

	public interface AddCatListener {
		public void onAddClick(String catName);

		public void onEdit(Category cat);
	}
}
